package com.vnt.controller;

import com.vnt.common.Result;
import com.vnt.entity.Role;
import com.vnt.entity.SysUser;
import com.vnt.service.SysRoleService;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class Controller {

    @Resource
    private SysRoleService sysRoleService;

    @PostMapping("/login")
    public Result<?> login(@RequestBody SysUser user){
        System.out.println("login");
        System.out.println(user.getUserName());
        System.out.println(user.getPassword());
        return Result.success("ok");
    }

    @PostMapping("/doMain")
    public Result<?> doMain(){
        // 当前系统时间
        long now = System.currentTimeMillis();
        long exp = now + 60 * 10  *1000;
        JwtBuilder jwtBuilder = Jwts.builder()
                .setId("8888")
                .setSubject("Rose")
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256,"1234")
                // 设置失效时间
                .setExpiration(new Date(exp));
        // 获取 jwt 的 token
        String token = jwtBuilder.compact();
        System.out.println(token);
        return Result.success(token);
    }

    @PostMapping("/toError")
    public Result toError(){
        System.out.println("toError");
        return Result.error("400","error");
    }

}
